<script type="text/javascript">

jQuery(document).ready(function($) {
    if ("{:session('email')}" == "") {
        if ("{:cookie('email')}" != "" && "{:cookie('remember_token')}" != "") {
            var $welcome = $('.subnav .welcome');
            var wtext = $welcome.html();
            $welcome.html(wtext + " (自动登录中...)");
            $.post('{:url("auth/autoLogin")}', function(data) {
                if (data.code) {
                    $welcome.html(data.msg+ ' 你好 (自动登陆成功, 页面将在3s后刷新...) <a href="javascript: ajax_logout();">注销</a>');
                    setTimeout(function() {
                        location.reload();
                    }, 3000);
                } else {
                    $welcome.html(wtext);
                }
            });
        }
    }
});

/**
 * ajax 方式注销
 * @author 杨栋森 mutoe@foxmail.com at 2016-07-19
 */
function ajax_logout() {
	$.post('{:url("auth/logout")}', function(data) {
		// 注销成功
		$alert = $('#am-alert');
        $alert.find('.content').text(data.msg);
        $alert.modal('open');
        if(data.code) {
            setTimeout(function() {
                location.href = data.url;
            }, data.wait * 500);
        }
	});
}

/**
 * 刷新验证码
 * @author 杨栋森 mutoe@foxmail.com at 2016-07-20
 * 需要将验证码 以下面的方式放置
 * <div id="captcha-img"><img src="{:captcha_src()}" alt="captcha" /></div>
 * 点击div#captcha-img后会重新拉取验证码
 */
$(document).on('click', '#captcha-img',function() {
    $.get('{:url("common/refreshCaptcha")}', function(data) {
        $('#captcha-img img').attr('src', data+ "?t="+ new Date().getTime())
    });
});

</script>